//
//  CCSlider.m
//  DiceGameBox2D
//
//  Created by jasonsalex on 13-1-29.
//


#import "CCSlider.h"

@implementation CCSlider

@synthesize maxValue;
@synthesize minValue;
@synthesize curValue;

+(id)sliderWithCCSlider:(id)button background:(id)bg target:(id)target callback:(SEL)cb
{
    return [[[self alloc] sliderWithInit:button background:bg target:target callback:cb]autorelease];
}


-(id)sliderWithInit:(id)button background:(id)bg target:(id)tar callback:(SEL)cb
{
    if(self=[super init])
    {
        target=tar;
        callBack=cb;
        
        curValue=0;
        maxValue=125;
        minValue=0;
        
        bgSprite=[CCSprite spriteWithFile:bg];
        bgSprite.anchorPoint=ccp(0,0.5);
        btnSprite=[CCSprite spriteWithFile:button];
        
        [btnSprite setPosition:ccp(0,bgSprite.boundingBox.size.height*0.5f-3)];
        
        [self addChild:bgSprite z:0];
        [self addChild:btnSprite z:2];
        
        curMove=[btnSprite position];
    }
    
    return self;
}

-(void)onEnter
{
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:-128 swallowsTouches:YES];
    [super onEnter];
}

-(void)onExit
{
    [[CCTouchDispatcher sharedDispatcher]removeDelegate:self];
    [super onExit];
}
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
   
    CGPoint touchLocal=[touch locationInView:[touch view]];
    touchLocal=[[CCDirector sharedDirector]convertToGL:touchLocal];
    
    touchLocal=[self convertToNodeSpace:touchLocal]; //转换为当前节点的坐标

    [self setCurValue: touchLocal.x];
    [target performSelector:callBack withObject:self];
    return CGRectContainsPoint([bgSprite boundingBox], touchLocal);
//    CGPoint selfPoint=[self convertToWorldSpace:self.position];
//    CGRect bBox = [bgSprite boundingBox];
//    bBox.origin= ccpAdd([bgSprite boundingBox].origin,selfPoint );
//    CCLOG(@"slider bounding:%@",NSStringFromCGRect(bBox));
//    CCLOG(@"touch local:%@",NSStringFromCGPoint(touchLocal));
//    if(CGRectContainsPoint(bBox, touchLocal))
//        CCLOG(@"YES");
//    else
//        CCLOG(@"NO");
//    return CGRectContainsPoint(bBox, touchLocal);
}

-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint touchMove=[touch locationInView:[touch view]];
    touchMove=[[CCDirector sharedDirector] convertToGL:touchMove];
    
    touchMove=[self convertToNodeSpace:touchMove];
    [self setCurValue: touchMove.x];
    [target performSelector:callBack withObject:self];
    
    /*
    CCLOG(@"touch move:%f",curValue);
    CGPoint touchMove=[touch locationInView:[touch view]];
    touchMove=[[CCDirector sharedDirector] convertToGL:touchMove];
    
    touchMove=[self convertToNodeSpace:touchMove];
    
    CGPoint subMove=ccpSub(touchMove, curMove);
    
    CGPoint moveTo=ccp(btnSprite.position.x+subMove.x,curMove.y);
    
    moveTo=[self convertToWorldSpace:moveTo];
    
    if(CGRectContainsPoint([self boundingBox], moveTo)) //判断是否超出了范围
    {
        [btnSprite setPosition:[self convertToNodeSpace:moveTo]];
        CGSize size=[self boundingBox].size;
        
        float def=maxValue-minValue;
        
        curValue=btnSprite.position.x*(def/size.width); //将当前滑块坐标转换为数值
        [target performSelector:callBack withObject:self];
    }
    
    curMove=[btnSprite position];
     */
}

-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    
    
}

-(void)setCurValue:(float)value //修改当前数值
{
    if(value<=maxValue&&value>=minValue)
    {
//        CGSize size=[self boundingBox].size;
//        
//        float def=maxValue-minValue;
//        
//        float x=(value/def)*size.width; //将数值转换为坐标点
        
        [btnSprite setPosition:ccp(value, btnSprite.position.y)];
        curValue=value;
    }
    
}

@end
